// ===================================
// SDF 头文件
// ===================================

#ifndef __SDF_H
#define __SDF_H

// 引入 GMSSL 库的头文件
#include <gmssl/sm3.h>
#include <gmssl/sm2.h>
#include <stdint.h> // 确保 uint8_t 等类型可用

// --- 1. 基础类型定义 ---
typedef unsigned char BYTE;
typedef unsigned char CHAR;
typedef int LONG;
typedef unsigned int ULONG;
typedef unsigned int FLAGS;
typedef CHAR * LPSTR;
typedef void * HANDLE;
typedef unsigned long ULONG_PTR; // 用于句柄转换

// --- 2. 算法标识符和错误码 ---
#define SGD_SM3 0x00000004
#define SDR_OK 0x00000000
#define SDR_BASE 0x01000000
#define SDR_COMMFAIL SDR_BASE + 0x00000003
#define SDR_ALGNOTSUPPORT SDR_BASE + 0x00000009
#define SDR_STEPERR SDR_BASE + 0x00000010
#define SDR_INARGERR SDR_BASE + 0x0000001D
#define SDR_OUTARGERR SDR_BASE + 0x0000001E
#define SDR_KEYNOTEXIST SDR_BASE + 0x0000001F
#define SDR_DECRYPT_FAIL 0x01000021 // 模拟解密失败的错误码


// --- 3. 结构体定义 ---

// 设备信息 (DEVICEINFO 结构体) - 完整定义
typedef struct DeviceInfo_st
{
    CHAR DeviceName[40];
    CHAR DeviceSerial[16];
    ULONG HashAlgAbility;
    ULONG SymAlgAbility;
    ULONG AymAlgAbility;
    ULONG BufferSize;
    ULONG Reserved;
} DEVICEINFO;


// ECC 结构体
#define ECCref_MAX_BITS 256 // SM2 使用 256 位
#define ECCref_MAX_LEN ((ECCref_MAX_BITS + 7) / 8) // 32 bytes

typedef struct ECCrefPublicKey_st
{
    ULONG bits;
    BYTE x[ECCref_MAX_LEN];
    BYTE y[ECCref_MAX_LEN];
} ECCrefPublicKey;

// ECCrefPrivateKey 结构体
typedef struct ECCrefPrivateKey_st
{
    ULONG bits;
    BYTE K[ECCref_MAX_LEN];
} ECCrefPrivateKey;

// ECC 密文结构体 (用于 SM2 加密结果 C1, C2, C3)
typedef struct ECCCipher_st
{
    BYTE C1[ECCref_MAX_LEN * 2 + 1]; // C1 65 bytes
    ULONG C2Len;
    BYTE C2[32]; // 密文数据区 (C2)，最大 32 bytes
    BYTE C3[32]; // 杂凑值 (C3)
} ECCCipher;


// --- 4. 关键外部变量声明 (用于测试) ---
// 允许测试文件访问内部存储的会话密钥
extern uint8_t session_keys[16][32]; 
// 允许测试文件访问内部的句柄索引函数
extern int get_key_index_from_handle(HANDLE hKey);


// --- 5. 函数声明 ---

// 设备管理
LONG SDF_OpenDevice(HANDLE * phDeviceHandle);
LONG SDF_CloseDevice(HANDLE hDeviceHandle);
LONG SDF_GetDeviceInfo(HANDLE hSessionHandle, DEVICEINFO *pstDeviceInfo);

// SM3 接口
LONG SDF_HashInit(HANDLE hSessionHandle, ULONG uiAlgID, ECCrefPublicKey *pucPublicKey, BYTE *pucID, ULONG uiIDLength);
LONG SDF_HashUpdate(HANDLE hSessionHandle, BYTE *pucData, ULONG uiDataLength);
LONG SDF_HashFinal(HANDLE hSessionHandle, BYTE *pucHash, ULONG *puiHashLength);

// ECC Key Export 
LONG SDF_ExportSignPublicKey_ECC(HANDLE hSessionHandle, ULONG uiKeyIndex, ECCrefPublicKey *pucPublicKey);
LONG SDF_ExportEncPublicKey_ECC(HANDLE hSessionHandle, ULONG uiKeyIndex, ECCrefPublicKey *pucPublicKey);

// ECC Key Encapsulation/Decapsulation
LONG SDF_GenerateKeyWithEPK_ECC(HANDLE hSessionHandle, ULONG uiKeyBits, ULONG uiAlgID, ECCrefPublicKey *pucPublicKey, ECCCipher *pucKey, HANDLE *phKeyHandle);
LONG SDF_ImportKeyWithISK_ECC(HANDLE hSessionHandle, ULONG uiISKIndex, ECCCipher *pucKey, HANDLE *phKeyHandle);

// 密钥销毁
LONG SDF_DestroyKey(HANDLE hSessionHandle, HANDLE hKeyHandle);

#endif
